/**
 * 用户注册登录
 */
$(document).ready(function() {
$('form[name="login"]').submit(function() {
	var email = $('input[name="email"]').val();
	var password = $('input[name="password"]').val();
	
	if(!email) {
		alert('请输入邮箱');return false;
	}
	if(!password) {
		alert('请输入密码');return false;
	}
	var params = $('form[name="login"]').serialize();
	$.post('/user/login.json', params, function(dt) {
		if(dt.status) {
			alert(dt.message);
		} else {
			window.location.href = '/user/';
		}
	}, 'json')
	return false;
});
$('form[name="registe"]').submit(function() {
	var email = $('input[name="email"]').val();
	var nickname = $('input[name="nickname"]').val();
	var password = $('input[name="password"]').val();
	var password1 = $('input[name="password1"]').val();
	
	if(!email) {
		alert('请输入邮箱');return false;
	}
	if(!nickname) {
		alert('请输入昵称');return false;
	}
	if(!password) {
		alert('请输入密码');return false;
	}
	if(password != password1) {
		alert('两次密码输入不一致');return false;
	}
	
	var params = $('form[name="registe"]').serialize();
	$.post('/user/registe.json', params, function(dt) {
		if(dt.status) {
			alert(dt.message);
		} else {
			$('form[name="registe"]').unbind("submit").attr('action', 'success').submit();
		}
	}, 'json')
	return false;
});
function resend() {
	if(!email) {
		alert('未知邮箱');return false;
	}
	$('a[name="resend"]').unbind("click");
	var params = {email: email};
	$.post('/user/resend.json', params, function(dt) {
		if(dt.status) {
			alert(dt.message);
		} else {
			clock._start = 10;
			clock.timer = setInterval(clock.run, 1000);
		}
	}, 'json');
}
$('a[name="resend"]').click(function(){resend()});

var clock = {
	_start : '',
	timer : '',
	run : function() {
		if(clock._start == 0) {
			clearInterval(clock.timer);
			$('a[name="resend"]').click(resend);
			$('#clock').html('');
		} else {
			clock._start--;
			$('#clock').html('('+clock._start+')');
		}
	}
};
})